package com.igroupes.ovineherd.model.attribute.number;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.igroupes.ovineherd.entity.DataModelAttributeEntity;
import com.igroupes.ovineherd.enums.BooleanKey;
import com.igroupes.ovineherd.exception.ModelException;
import com.igroupes.ovineherd.model.IParentModelData;
import com.igroupes.ovineherd.model.Style.NumberStyle;
import com.igroupes.ovineherd.model.dto.FieldApiResponse;
import com.igroupes.ovineherd.model.enums.MySqlTypeKey;
import com.igroupes.ovineherd.query.FieldQuery;
import com.igroupes.ovineherd.query.ModelDataJsonAttributeQuery;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 金钱
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ToString(callSuper = true)
public class MoneyModelData extends NumberBasicAttribute {

    public static final Integer MAXAGE = 999999999;
    public static final Integer MINAGE = 0;
    public static final String MINLABEL = "最小金额";
    public static final String MAXLABEL = "最大金额";
    private static final int DEFAULT_PRECISION = 2;

    private static String REGEX = "^[1-9][0-9]*$"; //有四种钱的表示形式我们可以接受:"10000.00" 和 "10,000.00", 和没有 "分" 的 "10000" 和 "10,000"
    public MoneyModelData() {
        super();
        this.minValue = new NumberStyle(MINLABEL, MINAGE, false,MINAGE);
        this.maxValue = new NumberStyle(MAXLABEL,MAXAGE, false,MINAGE, MAXAGE);
    }

    @Override
    public List<FieldQuery> sqlField(IParentModelData parentModelData, DataModelAttributeEntity dataModelAttributeEntityOld) {
        MoneyModelData moneyModelData = (MoneyModelData) parentModelData.getAttributes();
        List<FieldQuery> fieldQueries = super.sqlField(parentModelData,dataModelAttributeEntityOld);
        FieldQuery fieldQuery = fieldQueries.get(0);
        fieldQuery.setType(MySqlTypeKey.DECIMAL.getCode());
        int len = (int)Math.log10(Convert.toDouble(maxValue.getValue())) + 1 + DEFAULT_PRECISION;
        fieldQuery.setLength(len);
        fieldQuery.setPrecision(DEFAULT_PRECISION);
        fieldQueries.set(0,fieldQuery);
        return fieldQueries;
    }

    @Override
    public JSONObject getAttribute(ModelDataJsonAttributeQuery modelDataJsonAttributeQuery) {
        JSONObject jsonObject = super.getAttribute(modelDataJsonAttributeQuery);
        jsonObject.set("minValue",new NumberStyle(MINLABEL, modelDataJsonAttributeQuery.getJson().get("minValue"), false,MINAGE));
        jsonObject.set("maxValue",new NumberStyle(MAXLABEL,modelDataJsonAttributeQuery.getJson().get("maxValue"), false, MINAGE, MAXAGE));
        return jsonObject;
    }
    @Override
    public List<FieldApiResponse> verifyRequestData(DataModelAttributeEntity modelAttributeEntity, Object value) {
       
        List<FieldApiResponse> fieldApiResponses = new ArrayList<>();
        if(value == null){
            return fieldApiResponses;
        }
        BigDecimal valueDecimal = new BigDecimal(value.toString());
        JSONObject jsonObject = modelAttributeEntity.getJson();
        if(ObjectUtil.isNotNull(jsonObject.get("minValue"))){
            BigDecimal minValue = new BigDecimal(jsonObject.get("minValue").toString());
            if(valueDecimal.compareTo(minValue) < 0 ){
                throw new ModelException(String.format("%s值范围错误",modelAttributeEntity.getName()));
            }
        }
        if(ObjectUtil.isNotNull(jsonObject.get("maxValue"))) {
            BigDecimal maxValue = new BigDecimal(jsonObject.get("maxValue").toString());
            if (valueDecimal.compareTo(maxValue) > 0) {
                throw new ModelException(String.format("%s值范围错误", modelAttributeEntity.getName()));
            }
        }
        FieldApiResponse fieldApiResponse = new FieldApiResponse();
        fieldApiResponse.setDbName(modelAttributeEntity.getDbName());
        fieldApiResponse.setValue(value);
        fieldApiResponse.setIsStr(false);
        fieldApiResponse.setType(MySqlTypeKey.INT.getCode());
        fieldApiResponses.add(fieldApiResponse);
        return fieldApiResponses;
    }
    @Override
    public JSONObject getAttributeStyle(ModelDataJsonAttributeQuery dataModelAttributeEntity) {

        JSONObject jsonObject = dataModelAttributeEntity.getJson();
        Integer minValue = Convert.toInt(jsonObject.get("minValue"));
        Integer maxValue = Convert.toInt(jsonObject.get("maxValue"));
        return new JSONObject(new NumberStyle(
                dataModelAttributeEntity.getName(),"",
                minValue, maxValue, 2,
                dataModelAttributeEntity.getIsNull().equals(BooleanKey.TRUE.getCode())
        ), false);
    }
}